package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.validation.Validator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.system.domain.SysCourse;
import com.ruoyi.system.domain.SysStudentCourse;
import com.ruoyi.system.domain.SysTextbook;
import com.ruoyi.system.mapper.SysCourseMapper;
import com.ruoyi.system.mapper.SysStudentCourseMapper;
import com.ruoyi.system.mapper.SysTextbookMapper;
import com.ruoyi.system.service.ISysCourseService;

/**
 * 课程管理Service业务层处理
 * 
 * @author ruoyi
 */
@Service
public class SysCourseServiceImpl implements ISysCourseService 
{
    
    @Autowired
    private SysCourseMapper courseMapper;
    @Autowired
    private SysStudentCourseMapper sysStudentCourseMapper;
    @Autowired
    private SysTextbookMapper sysTextbookMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询课程
     * 
     * @param courseId 课程ID
     * @return 课程
     */
    @Override
    public SysCourse selectById(Long courseId)
    {
        return courseMapper.selectById(courseId);
    }

    /**
     * 查询课程列表
     * 
     * @param course 课程
     * @return 课程
     */
    @Override
    public List<SysCourse> selectCourseList(SysCourse course)
    {
        return courseMapper.list(course);
    }

    /**
     * 查询课程列表
     * 
     * @param course 课程
     * @return 课程
     */
    @Override
    public List<SysCourse> listByStudentId(Long studentId)
    {
        return courseMapper.listByStudentId(studentId, null);
    }

    /**
     * 新增课程
     * 
     * @param course 课程
     * @return 结果
     */
    @Override
    public int insertCourse(SysCourse course)
    {
        return courseMapper.insert(course);
    }

    /**
     * 修改课程
     * 
     * @param course 课程
     * @return 结果
     */
    @Override
    public int updateCourse(SysCourse course)
    {
        return courseMapper.update(course);
    }

    /**
     * 批量删除课程
     * 
     * @param courseIds 需要删除的课程ID
     * @return 结果
     */
    @Override
    public int deleteCourseByIds(Long[] courseIds)
    {
        return courseMapper.deleteByIds(courseIds);
    }

    /**
     * 删除课程信息
     * 
     * @param courseId 课程ID
     * @return 结果
     */
    @Override
    public int deleteCourseById(Long courseId)
    {
        return courseMapper.deleteById(courseId);
    }

    /**
     * 导入用户数据
     * 
     * @param list 用户数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importCourse(List<SysCourse> list, String operName, String courseType)
    {
        if (StringUtils.isNull(list) || list.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (SysCourse entity : list)
        {
            try
            {
                BeanValidators.validateWithException(validator, entity);
                entity.setCreateBy(operName);
//                entity.setCourseType(courseType);
                courseMapper.insert(entity);
                successNum++;
                successMsg.append("<br/>" + successNum + "、课程名称 " + entity.getCourseName() + " 导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                e.printStackTrace();
//                String msg = "<br/>" + failureNum + "、账号 " + user.getUserName() + " 导入失败：";
//                failureMsg.append(msg + e.getMessage());
//                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    /**
     * 查询已分配班级的学员列表
     * 
     * @param classId 班级ID
     * @return 用户列表
     */
    @Override
    public List<SysCourse> listCourseForCheckbox(String courseType)
    {
    	SysCourse course = new SysCourse();
    	if (courseType != null && !"all".equals(courseType)) {
	    	course.setCourseType(courseType);
		}
        return courseMapper.list(course);
    }

    /**
     * 查询已分配班级的学员列表
     * 
     * @param classId 班级ID
     * @return 用户列表
     */
    @Override
    public List<SysStudentCourse> listCourseOfStudent(Long studentId)
    {
        return sysStudentCourseMapper.listByStudentId(studentId, null);
    }

    /**
     */
    @Override
    public List<TreeSelect> textbookTreeList()
    {
        
        // 添加顶级【全部教材】
        List<TreeSelect> listRoot = new ArrayList<TreeSelect>();
        {
        	Long id = 0L;
        	String label = "全部教材";
        	boolean disabled = false;
        	Integer lev = 0;
        	List<TreeSelect> children = getTextbookTree();
            listRoot.add(new TreeSelect(id, label, disabled, lev, children));
        }
        return listRoot;
    }
    
    public List<TreeSelect> getTextbookTree()
    {
        List<SysTextbook> listSysTextbook = sysTextbookMapper.selectList(new SysTextbook());
        
        // 添加教材节点列表
        List<TreeSelect> listTextbookTree = new ArrayList<TreeSelect>();
        for(SysTextbook sysTextbook : listSysTextbook) {
        	Long id = sysTextbook.getTextbookId();
        	String label = sysTextbook.getTextbookName();
        	boolean disabled = false;
        	Integer lev = 1;
        	List<TreeSelect> children = null;
        	listTextbookTree.add(new TreeSelect(id, label, disabled, lev, children));
        }
        return listTextbookTree;
    }
}